local skel = fk.CreateSkill{
  name = "ksp__xianxing",
}

Fk:loadTranslationTable{
  ["ksp__xianxing"] = "衔星",
  [":ksp__xianxing"] = "每回合每种花色限重铸一张，当你造成或受到伤害后，你可以令一名未横置的角色重铸任意张牌；若其不能重铸，你摸此技能剩余花色数张牌，其横置。",
 
  ["@ksp__xianxing-turn"] = "衔星",
  ["#ksp__xianxing"] = "衔星：你可以令一名未横置的角色重铸任意张牌；若其不能重铸，你摸 %arg 张牌，其横置",
  ["#ksp__xianxing-recast"] = "衔星：请重铸至少一张牌（每种花色至多一张）",
  ["#ksp__xianxing-nil"] = "衔星：请重铸至少一张牌（每种花色至多一张，不能为 %arg ）",

  ["$ksp__xianxing1"] = "天炎做笔称翊赞，待兵锋回转，赤遍中原。",
  ["$ksp__xianxing2"] = "宗刘继纳非鼎沸，许宏渺生民，尽我血泪。",
}
local xianxing_spec = {
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local ts = table.filter(room:getAlivePlayers(),function (t)
      return not t.chained
    end)
    local mark = player:getTableMark("@ksp__xianxing-turn")
    if #ts == 0 or #mark > 3 then return end
    local ps = room:askToChoosePlayers(player,{
      targets = ts,
      min_num = 1,
      max_num = 1,
      prompt = "#ksp__xianxing:::"..(4 - #mark),
      skill_name = skel.name,
    })
    if #ps > 0 then
      local t = ps[1]
      local ids = table.filter(t:getCardIds("he"),function (id)
        return not table.contains(mark,Fk:getCardById(id):getSuitString(true))
      end)
      if #ids > 0 then
        local ret_data = {
          skillName = skel.name,
          cancelable = false,
          extra_data = {
            ids = ids,
          },
        }
        local prompt = "#ksp__xianxing-recast"
        if #mark > 0 then
          local text = ""
          for i, suit in ipairs(mark) do
            text = text..Fk:translate(suit)
            if i < #mark then
              text = text.."/"
            end
          end
          prompt = "#ksp__xianxing-nil:::"..text
        end
        local success, ret = room:askToUseActiveSkill(t, {
          skill_name = "#ksp__xianxing_choose",
          prompt = prompt,
          cancelable = false,--禁止取消需要在data里也声明
          extra_data = ret_data,
        })
        if success and ret and ret.cards then
          ids = ret.cards
        else
          ids = {ids[1]}
        end
      end
      event:setCostData(self,{to = t,cards = ids})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self)
    local to = cost_data.to
    local cards = cost_data.cards
    if #cards > 0 then
      room:recastCard(cards,to,skel.name)
      for _, id in ipairs(cards) do
        room:addTableMarkIfNeed(player,"@ksp__xianxing-turn",Fk:getCardById(id):getSuitString(true))
      end
    else
      player:drawCards(4 - #player:getTableMark("@ksp__xianxing-turn"),skel.name)
      if not to.dead then
        to:setChainState(true)
        --to:drawCards(1,skel.name)
      end
    end
  end,
}

skel:addEffect(fk.Damage, {
  can_trigger = function(self, event, target, player, data)
    return (player:hasSkill(skel.name) and target == player) or table.contains((player.room:getTag("ksp__zhuochen") or {}),player.id)
  end,
  on_cost = xianxing_spec.on_cost,
  on_use = xianxing_spec.on_use,
})

skel:addEffect(fk.Damaged, {
  can_trigger = function(self, event, target, player, data)
    return (player:hasSkill(skel.name) and target == player)
  end,
  on_cost = xianxing_spec.on_cost,
  on_use = xianxing_spec.on_use,
})

return skel